﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;

namespace WebUI.Reservation
{
    public partial class CheckLabStatus : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            Calendar1.MinDate = DateTime.Now;
            Calendar1.MaxDate = DateTime.Now.AddDays(14);
            ExtAspNet.ListItem item = new ExtAspNet.ListItem("请选择", "-1");
            dl_SelectLocation.DataSource = BLL.Parameter.GetParamList("10");
            dl_SelectLocation.DataTextField = "value";
            dl_SelectLocation.DataValueField = "id";
            dl_SelectLocation.DataBind();
            dl_SelectLocation.Items.Insert(0, item);
        }

        protected void dl_SelectLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            dl_SelectLab.Hidden = false;
            ExtAspNet.ListItem item = new ExtAspNet.ListItem("请选择", "-1");
            dl_SelectLab.DataSource = BLL.LabManage.GetLabList(" location = " + dl_SelectLocation.SelectedValue);
            dl_SelectLab.DataTextField = "LabName";
            dl_SelectLab.DataValueField = "LabID";
            dl_SelectLab.DataBind();
            dl_SelectLab.Items.Insert(0, item);
        }

        protected void Calendar1_DateSelect(object sender, EventArgs e)
        {
            if (dl_SelectLocation.SelectedValue != "-1" && dl_SelectLab.SelectedValue != "-1")
            {
                Grid1.Title = dl_SelectLocation.SelectedText + " " + dl_SelectLab.SelectedText + " " + string.Format("{0:D}", Calendar1.SelectedDate.Value) + " 预约状态";
                BindGrid(int.Parse(dl_SelectLab.SelectedValue), Calendar1.SelectedDate.Value);
            }
            else
            {
                Alert.Show("请先选择实验楼、实验室，再选择日期！");
            }
        }

        private void BindGrid(int OpenLabID, DateTime date)
        {
            DataTable dt = new DataTable();
            DataColumn Column1 = new DataColumn("lesson", typeof(String));
            DataColumn Column2 = new DataColumn("lessonID", typeof(Int32));
            DataColumn Column3 = new DataColumn("LabName", typeof(String));
            DataColumn Column4 = new DataColumn("LabID", typeof(Int32));
            DataColumn Column5 = new DataColumn("date", typeof(DateTime));
            DataColumn Column6 = new DataColumn("isOpen", typeof(Boolean));
            dt.Columns.Add(Column1);
            dt.Columns.Add(Column2);
            dt.Columns.Add(Column3);
            dt.Columns.Add(Column4);
            dt.Columns.Add(Column5);
            dt.Columns.Add(Column6);
            DataRow row;
            if (BLL.OpenLabStatusManage.Exists(OpenLabID, date))
            {
                Model.OpenLabStatus m = BLL.OpenLabStatusManage.SelectOneByPrimaryKey(OpenLabID, date);
                if (m.lesson12.ToString() == "")
                {
                    row = dt.NewRow();
                    row[0] = "1,2节课";
                    row[1] = 56;
                    row[2] = dl_SelectLab.SelectedText;
                    row[3] = OpenLabID;
                    row[4] = date;
                    row[5] = true;
                    dt.Rows.Add(row);
                }
                if (m.lesson34.ToString() == "")
                {
                    row = dt.NewRow();
                    row[0] = "3,4节课";
                    row[1] = 57;
                    row[2] = dl_SelectLab.SelectedText;
                    row[3] = OpenLabID;
                    row[4] = date;
                    row[5] = true;
                    dt.Rows.Add(row);
                }
                if (m.lesson56.ToString() == "")
                {
                    row = dt.NewRow();
                    row[0] = "5,6节课";
                    row[1] = 58;
                    row[2] = dl_SelectLab.SelectedText;
                    row[3] = OpenLabID;
                    row[4] = date;
                    row[5] = true;
                    dt.Rows.Add(row);
                }
                if (m.lesson78.ToString() == "")
                {
                    row = dt.NewRow();
                    row[0] = "7,8节课";
                    row[1] = 58;
                    row[2] = dl_SelectLab.SelectedText;
                    row[3] = OpenLabID;
                    row[4] = date;
                    row[5] = true;
                    dt.Rows.Add(row);
                }
            }
            else
            {
                row = dt.NewRow();
                row[0] = "1,2节课";
                row[1] = 56;
                row[2] = dl_SelectLab.SelectedText;
                row[3] = OpenLabID;
                row[4] = date;
                row[5] = true;
                dt.Rows.Add(row);

                row = dt.NewRow();
                row[0] = "3,4节课";
                row[1] = 57;
                row[2] = dl_SelectLab.SelectedText;
                row[3] = OpenLabID;
                row[4] = date;
                row[5] = true;
                dt.Rows.Add(row);

                row = dt.NewRow();
                row[0] = "5,6节课";
                row[1] = 58;
                row[2] = dl_SelectLab.SelectedText;
                row[3] = OpenLabID;
                row[4] = date;
                row[5] = true;
                dt.Rows.Add(row);

                row = dt.NewRow();
                row[0] = "7,8节课";
                row[1] = 58;
                row[2] = dl_SelectLab.SelectedText;
                row[3] = OpenLabID;
                row[4] = date;
                row[5] = true;
                dt.Rows.Add(row);
            }
            Grid1.DataSource = dt;
            Grid1.DataBind();
        }
    }
}